package com.sandasai.service.impl;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sandasai.dao.AdminDao;
import com.sandasai.dao.MarkDao;
import com.sandasai.dao.NoticeDao;
import com.sandasai.dao.SettingDao;
import com.sandasai.dao.StudentDao;
import com.sandasai.dao.TeacherDao;
import com.sandasai.dao.UserDao;
import com.sandasai.dao.WorkDao;
import com.sandasai.dao.WorktypeDao;
import com.sandasai.domain.Bankcard;
import com.sandasai.domain.Mark;
import com.sandasai.domain.Student;
import com.sandasai.domain.Teacher;
import com.sandasai.domain.User;
import com.sandasai.domain.Work;
import com.sandasai.service.TeacherManager;
import com.sandasai.vo.MarkBean;
import com.sandasai.vo.StudentBean;
import com.sandasai.vo.TeacherBean;
import com.sandasai.vo.WorkInfoForMark;
@Service("teacherManager")
@Transactional
public class TeacherManagerImpl implements TeacherManager {
    @Resource
    private UserDao userDao;
    @Resource
    private StudentDao studentDao;
    @Resource
    private TeacherDao teacherDao;
    @Resource
    private AdminDao adminDao;
    @Resource
    private WorktypeDao worktypeDao;
    @Resource
    private WorkDao workDao;
    @Resource
    private MarkDao markDao;
    @Resource
    private SettingDao settingDao;
    @Resource
    private NoticeDao noticeDao;
    
    @Override
    public boolean updateInfo(TeacherBean teacherBean) {
        User user = userDao.findByUsername(teacherBean.getUsername());
        if(user == null) return false;
        Teacher teacher = user.getTeacher();
        Bankcard bankCard;
        if(user.getBankcard() == null) {
        	bankCard = new Bankcard();
        }else {
        	bankCard = user.getBankcard();
        }
        bankCard.setBankCardNum(teacherBean.getBankCardId());
        bankCard.setUser(user);
        user.setBankcard(bankCard);
        teacher.setJobId(teacherBean.getJobId());
        teacher.setPosition(teacherBean.getPosition());
        teacher.setRealName(teacherBean.getRealName());
        teacher.setTel(teacherBean.getTel());
        teacher.setVerified(false);
        teacher.setWorkUnit(teacherBean.getWorkUnit());
        teacherDao.update(teacher);
        return true;
    }
    @SuppressWarnings("unchecked")
    @Override
	public List<WorkInfoForMark> getWorkInfoForMarks(String username) {
        User user = userDao.findByUsername(username);
        Teacher teacher = user.getTeacher();
        List<Mark> marks = new LinkedList<Mark>(teacher.getMarks());
        List<WorkInfoForMark> workInfoForMarks = new LinkedList<WorkInfoForMark>();
        for(Mark mark : marks) {
            WorkInfoForMark workInfoForMark = new WorkInfoForMark();
            Work work = mark.getWork();
            workInfoForMark.setWorkId(work.getId());
            workInfoForMark.setTitle(work.getTitle());
            workInfoForMark.setScore(mark.getScore());
            workInfoForMark.setMarkId(mark.getId());
            workInfoForMarks.add(workInfoForMark);
            
        }
        return workInfoForMarks;
    }
	@Override
	public TeacherBean getTeacherBean(String username) {
		User user = userDao.findByUsername(username);
        if(user == null) {
        	return null;
        }
        Teacher teacher = user.getTeacher();
        TeacherBean teacherBean = new TeacherBean();
        if(user.getBankcard() == null) {
        	teacherBean.setBankCardId("");
        }else {
        	teacherBean.setBankCardId(teacher.getUser().getBankcard().getBankCardNum());
        }
        teacherBean.setRealName(teacher.getRealName());
        teacherBean.setJobId(teacher.getJobId());
        teacherBean.setPosition(teacher.getPosition());
        teacherBean.setTel(teacher.getTel());
        teacherBean.setWorkUnit(teacher.getWorkUnit());
        return teacherBean;
	}
	
	@Override
	public List<MarkBean> getMarkBeans(String username) {
		User user = userDao.findByUsername(username);
		Teacher teacher = user.getTeacher();
		List<Mark> marks = new LinkedList<Mark>(teacher.getMarks());
		List<MarkBean> markBeans = new LinkedList<MarkBean>();
		for(Mark mark : marks) {
			MarkBean markBean = new MarkBean();
			Work work = mark.getWork();
			markBean.setWorkId(work.getId());
			markBean.setWorkFileURL("/download.action?fileName=" + work.getFileName());
			markBean.setComment(mark.getComment());
			markBean.setScore(mark.getScore());
			markBean.setMarkId(mark.getId());
			markBean.setWorkType(work.getWorktype().getTypeName());
			markBean.setWorkTitle(work.getTitle());
			markBeans.add(markBean);
		}
		return markBeans;
	}
	@Override
	public boolean mark(String username, Integer markId, Double score,
			String comment) {
		try {
			Mark mark = markDao.findById(markId);
			mark.setScore(score);
			mark.setComment(comment);
			markDao.update(mark);
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}
}